package com.deer.wms.lzb.erp.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.lzb.erp.dao.ErpRecordMapper;
import com.deer.wms.lzb.erp.model.ErpRecord;
import com.deer.wms.lzb.erp.model.ErpRecordCriteria;
import com.deer.wms.lzb.erp.service.ErpRecordService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wei
 * @date 2021/02/14
 */
@Service
@Transactional
public class ErpRecordServiceImpl extends SuperServiceImpl<ErpRecordMapper, ErpRecord> implements ErpRecordService {

    @Autowired
    private ErpRecordMapper erpRecordMapper;

    @Override
    public List<ErpRecord> findList(ErpRecordCriteria criteria) {
        return erpRecordMapper.findList(criteria);
    }

    @Autowired
    private ErpRecordService erpRecordService;

    @Value("${lzb.erp.baseUrl}")
    private String baseUrl;
    @Value("${lzb.erp.inUrl}")
    private String inUrl;
    @Value("${lzb.erp.outUrl}")
    private String outUrl;
    @Value("${lzb.erp.returnUrl}")
    private String returnUrl;
    @Value("${lzb.erp.token}")
    private String token;


    public  Map<String, String> requestERP( String body, String url) {
        Map<String, String> map = new HashMap<>();
        try{
            url =baseUrl+url;
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            headers.add("access-token", token);
            HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
            RestTemplate client = new RestTemplate();
            ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
            String res = response.getBody();
            JSONObject object = JSON.parseObject(res);
            Boolean success = object.getBoolean("success");
            String code = object.getString("code");
            String msg = object.getString("msg");
            map.put("success", success != null ? success.toString() : "false");
            map.put("msg", msg);
            map.put("code", code);
        }catch (Exception ex){
            map.put("success", "false");
            map.put("msg", ex.getMessage());
            map.put("code", "500");
        }
        return map;
    }


    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void erpLog(Map<String, Object> returnMap, int type) {
        //添加回传日志信息
        ErpRecord erpBackRecord = new ErpRecord();
        erpBackRecord.setCreateTime(DateUtil.now());
        erpBackRecord.setParam(returnMap.get("data")+"");
        erpBackRecord.setMsg(returnMap.get("msg")+"");
        erpBackRecord.setCode(returnMap.get("code")+"");
        erpBackRecord.setType(type); //业务类型
        if (!erpRecordService.save(erpBackRecord)) {
            throw new ServiceException(CommonCode.SERVER_INERNAL_ERROR, "添加日志失败");
        }
    }

}
